<?php
namespace app\admin\controller;
use think\facade\Db;
use think\facade\Cache;
use app\common\model\Member as MemberModel;
use app\common\model\UserMoneyLog as UserMoneyLogModel;
use think\cache\driver\Redis;

class Index extends Admin {
	
	
	public function index(){
		return view('index');
	}


    //后台首页主体内容
    public function main(){
        if(!$this->request->isPost()){
            return view('main');
        }else{
            $redis = new Redis(config('cache.stores.redis'));

            $start_time = strtotime(date('Y-m-d') . ' +1 day') - 86400 * 30;
            $start_day = date('d',$start_time);
            $last_time = strtotime(date('Y-m-d') . ' +1 day')-1;
            $last_day = date('d',$last_time);
            $month = date('Y-m',$start_time).'至'.date('Y-m',$last_time);
            $day = [
                $start_day,
                date('d',$start_time+86400*1),
                date('d',$start_time+86400*2),
                date('d',$start_time+86400*3),
                date('d',$start_time+86400*4),
                date('d',$start_time+86400*5),
                date('d',$start_time+86400*6),
                date('d',$start_time+86400*7),
                date('d',$start_time+86400*8),
                date('d',$start_time+86400*9),
                date('d',$start_time+86400*10),
                date('d',$start_time+86400*11),
                date('d',$start_time+86400*12),
                date('d',$start_time+86400*13),
                date('d',$start_time+86400*14),
                date('d',$start_time+86400*15),
                date('d',$start_time+86400*16),
                date('d',$start_time+86400*17),
                date('d',$start_time+86400*18),
                date('d',$start_time+86400*19),
                date('d',$start_time+86400*20),
                date('d',$start_time+86400*21),
                date('d',$start_time+86400*22),
                date('d',$start_time+86400*23),
                date('d',$start_time+86400*24),
                date('d',$start_time+86400*25),
                date('d',$start_time+86400*26),
                date('d',$start_time+86400*27),
                date('d',$start_time+86400*28),
                $last_day
            ];

            $kai = strtotime(date('Y-m-d',time()));

            //折线图数据
            $echat_data = [
                'usdt'   =>  [
                    'title'=>'30天业绩折线图',
                    'day'=>$day,	//每月天数
                ],
                'kp'   =>  [
                    'title'=>'30天业绩折线图',
                    'day'=>$day,	//每月天数
                ],
                'lp'  =>  [
                    'title'=>'30天业绩折线图',
                    'day'=>$day,	//每月天数
                ],
            ];

            $usdt_kData = Db::Query("SELECT
                    SUM(num) AS num,
                    FROM_UNIXTIME(add_time, '%d') AS day
                FROM 
                    cd_user_money_log
                WHERE add_time > $start_time and currency = 'adc'
                GROUP BY 
                    FROM_UNIXTIME(add_time, '%Y-%m-%d 00:00:00')
                ");
            $kp_kData = Db::Query("SELECT
                    SUM(num) AS num,
                    FROM_UNIXTIME(add_time, '%d') AS day
                FROM 
                    cd_user_money_log
                WHERE add_time > $start_time and currency = 'usdt'
                GROUP BY 
                    FROM_UNIXTIME(add_time, '%Y-%m-%d 00:00:00')
                ");
            $lp_kData = Db::Query("SELECT
                    SUM(num) AS num,
                    FROM_UNIXTIME(add_time, '%d') AS day
                FROM 
                    cd_user_money_log
                WHERE add_time > $start_time and currency = 'adc'
                GROUP BY 
                    FROM_UNIXTIME(add_time, '%Y-%m-%d 00:00:00')
                ");

            foreach ($usdt_kData as $v){
                $echat_data['usdt']['data'][] = $v['num'];
            }
            foreach ($kp_kData as $v){
                $echat_data['kp']['data'][] = $v['num'];
            }
            foreach ($lp_kData as $v){
                $echat_data['lp']['data'][] = $v['num'];
            }

            if(config('my.show_home_chats',true)){
                $data['echat_data'] = $echat_data;
            }
            $data['card_data'] = $this->getCardData();
            $data['status'] = 200;
            $data['start_day'] = $start_day;
            $data['last_day'] = $last_day;
            $data['month'] = $month;
            return json($data);
        }
    }
	
	
	
	//首页统计数据
	private function getCardData(){
        $redis = new Redis(config('cache.stores.redis'));
		$card_data = [	//头部统计数据
			[
			  'title_icon'=>"el-icon-user",
			  'card_title'=> "全网USDT",
			  'card_cycle'=> "持",
			  'card_cycle_back_color'=> "#409EFF",
			  'bottom_title'=> "最大持有",
			  'vist_num'=> MemberModel::count_wallet('usdt'),
			  'vist_all_num'=> MemberModel::max_wallet('usdt'),
			  'vist_all_icon'=> "el-icon-trophy",
			],
            [
                'title_icon'=>"el-icon-user",
                'card_title'=> "ADC销毁",
                'card_cycle'=> "总",
                'card_cycle_back_color'=> "#409EFF",
                'bottom_title'=> "总算力",
                'vist_num'=> $redis->get('adc_destroy'),
                'vist_all_num'=> $redis->get('total_arithmetical'),
                'vist_all_icon'=> "el-icon-trophy",
            ],
			[
			  'title_icon'=> "el-icon-download",
			  'card_title'=> "全网ADC",
			  'card_cycle'=> "持",
			  'card_cycle_back_color'=> "#67C23A",
			  'bottom_title'=> "最大持有",
			  'vist_num'=> MemberModel::count_wallet('adc'),
			  'vist_all_num'=> MemberModel::max_wallet('adc'),
			  'vist_all_icon'=> "el-icon-download",
			],
			[
			  'title_icon'=> "el-icon-wallet",
			  'card_title'=> "总全网算力",
			  'card_cycle'=> "总",
			  'card_cycle_back_color'=> "#F56C6C",
			  'bottom_title'=> "最大持有算力",
			  'vist_num'=> $redis->get('total_arithmetical'),
			  'vist_all_num'=> $redis->get('max_member_arithmetical'),
			  'vist_all_icon'=> "el-icon-coin",
			],
			[
			  'title_icon'=> "el-icon-coordinate",
			  'card_title'=> "新增用户",
			  'card_cycle'=> "日",
			  'card_cycle_back_color'=> "#E6A23C",
			  'bottom_title'=> "总用户",
			  'vist_num'=> MemberModel::where('status',1)->whereDay('create_time')->count('id'),
			  'vist_all_num'=> MemberModel::where('status',1)->count('id'),
			  'vist_all_icon'=> "el-icon-data-line",
			],
		];
		
		return $card_data;
	}
	
	
	
	
}